package edu.jsnu.purchase.ticket.service.impl;

import edu.jsnu.purchase.ticket.service.FilmService;
import edu.jsnu.service.ticket.film.dao.FilmMapper;
import edu.jsnu.service.ticket.film.domain.Film;
import edu.jsnu.service.ticket.film.dto.FilmBrief;
import edu.jsnu.service.ticket.film.dto.FilmDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

@Service
public class FilmServiceImpl implements FilmService {
    @Autowired
    private FilmMapper filmMapper;

    @Override
    public List<FilmBrief> getBriefFilms(Integer page, Integer pageCount, Integer flag, String kind, String year) {
        Integer startPage = (page - 1)*pageCount;
        List<FilmBrief> filmList = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.DAY_OF_YEAR, calendar.get(Calendar.DAY_OF_YEAR) + 7);
        Date endTime = calendar.getTime();
        if (flag == 2){//即将上映的影片  包括已经被安排在场次中7天以外，加上未被安排到场次中的影片
            filmList = filmMapper.getBriefFilmInfo7DaysLater(startPage, pageCount, kind, year, endTime);
        }else if (flag == 1){//正在热播的影片
            filmList = filmMapper.getBriefFilmInfoToEndDate(startPage, pageCount, kind, year, endTime);
        }
        return filmList;
    }

    @Override
    public Integer getTotalCount() {
        return filmMapper.getTotalCount();
    }

    @Override
    public FilmDto getFilmById(Integer id) {
        FilmDto film = filmMapper.getFilmDtoById(id);
        return film;
    }

    @Override
    public List<Film> getHotFilms(){
        List<Film> films = filmMapper.getHotFilms();
        if (films == null || films.isEmpty()){
            return new ArrayList<>();
        }
        return films;
    }

}
